<template>
  <!-- 添加一个 div 用作人机身份验证容器 -->
      <div id="grecaptcha"></div>
</template>
<script>
	import Utils from '../../assets/js/util.js'
	export default {
		name: 'Recaptcha',
		data() {
			return {
				sitekey: "6LdbjScaAAAAADAk6an6AmSAvHQYnskgW-Xgt_Ew"
			};
		},
		methods: {
			verification () {
				Utils.$emit('verification', 'userLogin')
			},
			submit: function(token) {
				var vm = this
				this.axios({
					method: 'GET',
					url: 'http://localhost/admin/auth/check',
					params: { googleToken: token }
				}).then(function(resp) {
					if (resp.data.status === 'ok'){
						vm.$message({
							message: '人机身份验证成功!',
							type: 'success',
							center: true
						});
						vm.verification()
					} else {
						vm.$message({
							message: '人机身份验证失败!',
							type: 'error',
							center: true
						});
					}
				})
			},
			loaded() {
				setTimeout(() => {
					window.grecaptcha.render("grecaptcha", {
						sitekey: this.sitekey,
						callback: this.submit
					});
				}, 200);
			}
		},
		mounted() {
			this.loaded();
		}
	};
</script>
